<?php namespace Home\Controller; 
use Hdphp\Controller\Controller;
use \Common\Model\Individual;
use \Common\Model\Address;
use \Common\Model\Indent;
use \Common\Model\IndentList;
//个人中心控制器
class IndividualController extends Controller{
	//实例化Model中的Individual类
	private $model;
    public function __init(){
    	$this->model = new Individual;
    }
	
    public function index(){
    	$uid = $_SESSION['user']['uid'];
		//获取旧数据
		$data = $this->model->where('uid',$uid)->first();
		$data['signTime'] = date('Y/m/d H:i:s',$data['signTime']);	
		//获取上次登陆时间
		//判断是否为第一次登录，若为第一次登录，则显示'—— ——'.
		if($_SESSION['user']['oldLoginTime'] == 0){
			$data['loginTime'] = '—— ——';
		}else{
			$data['loginTime']= date('Y/m/d H:i:s',$_SESSION['user']['oldLoginTime']);										
		}										
		//获取收货地址
		$addressModel = new Address;
		$addressData = $addressModel->where('user_uid',$uid)->get();
		//把user_address 表的address值改成数组形式，用于传递给js
		foreach($addressData as $k=>$v){
			//将字符串转成数组形式
			$addressData[$k]['address'] = explode('|', $v['address']);
		}		
		//订单
		$indentModel = new Indent;
		$indentData = $indentModel->where('user_uid',$uid)->get();
		$orderData = Db::table('indent_list il')
						->join('indent i','il.indent_iid','=','i.iid')
						->join('goods g','il.goods_gid','=','g.gid')
						->where("user_uid={$uid}")
						->orderBy('time','DESC')
						->get();
		//订单数量
		$indentNum = count($indentData);
		
		$orderDataNum = [];
		foreach($orderData as $v){
			$orderDataNum[$v['iid']][] = $v;
		}
		//组合订单清单
		$orderDataType = [];
		foreach($orderDataNum as $k=>$v){
			$time = $indentModel->where('iid',$k)->pluck('time');
			$time = date('Y/m/d H:i:s',$time);
			$orderDataType[]= [
				'inumber'    => $indentModel->where('iid',$k)->pluck('inumber'),
				'time'       => $time,
				'consignee'  => $indentModel->where('iid',$k)->pluck('consignee'),
				'total'      => $indentModel->where('iid',$k)->pluck('total'),
				'status'     =>	$indentModel->where('iid',$k)->pluck('status'),
				'value'      => $v
			];
		}
		
		View::with('data',$data);
		View::with('addressData',$addressData);
		View::with('indentNum',$indentNum);
		View::with('orderDataType',$orderDataType);
    	View::make();
    }
	
	//个人信息显示
	public function personalShow(){
		//获取异步传来的用户id
		$uid = q('post.uid');
		//获取旧数据
		$data = $this->model->where('uid',$uid)->first();
		//地址 字符串变成数组
		$data['address_detail'] = explode('|', $data['address_detail']);
		$data['province'] = $data['address_detail'][0];
		$data['city'] = $data['address_detail'][1];
		$data['county'] = $data['address_detail'][2];
		//出生日期 字符串变成数组
		$data['birthday'] = explode('-', $data['birthday']);
		$data['year'] = $data['birthday'][0];
		$data['month'] = $data['birthday'][1];
		$data['day'] = $data['birthday'][2];		
		//返给前台js
		echo json_encode($data);exit;
	}
	
	//编辑个人信息
	public function personalEdit(){
		if(IS_POST){
			//调用模型文件里的Type类的store方法去执行数据添加
			if($this->model->edit()){
				//若模型类中数据验证正确，成功
				View::success('个人信息修改成功');
			}
			//提示错误
			View::error($this->model->getError());
		}		
	}
		
	//修改密码
	public function changePassword(){
		if(IS_POST){
			/*1.判断原密码是否正确*/
			//获取用户输入的旧密码
			$oldpsd = q('post.oldPassword',null,'md5');
			//获取当前用户的数据库信息($session里的值)
			$uid = $_SESSION['user']['uid'];			
			$data = $this->model->where('uid',$uid)->get();
			//旧密码验证
			if($oldpsd!=$data[0]['password']){
				View::error('旧密码输入错误');
			}
			/*2.新密码验证*/
			$newpsd = q('post.newPassword',null);
			//验证新密码长度
			$newpsdLeng = strlen($newpsd);
			if($newpsdLeng < 6){
				View::error('新密码长度不得少于4位');
			}
			//正则判断新密码由字母、数字、下划线组成，且长度在  之间
			if(preg_match("/^\w{".$newpsdLeng.",15}+$/",$newpsd) == 0){
				View::error('新密码输入错误,应以字母、数字、下划线组成');
			}
			//验证两次新密码输入是否一致
			$connewpsd = q('post.confirmPassword',null);
			if($newpsd!=$connewpsd){
				View::error('两次密码输入不一致');
			}
			//更新数据库
			$this->model->where('uid',$uid)->update(['password'=>md5($newpsd)]);
			//调回登录页面
			session_unset($_SESSION['user']);
			session_destroy();
			go(U('Member/index'));			
			exit;	
		}	    
	}
	
	//添加收货地址
	public function addressAdd(){
		$addressModel = new Address;	
		if(IS_POST){
			if($addressModel->store()){
				//若模型类中数据验证正确，成功
				View::success('收货地址添加成功');
			}
			//提示错误
			View::error($addressModel->getError());
		}
	}
		
	//修改地址时显示旧数据
	public function addressShow(){			
		$uaid = q('post.uaid');
		$addressModel = new Address;
		$addressData = $addressModel->where('uaid',$uaid)->find();
		//地址 字符串变成数组
		$addressData['address'] = explode('|', $addressData['address']);
		$addressData['province'] = $addressData['address'][0];
		$addressData['city'] = $addressData['address'][1];
		$addressData['county'] = $addressData['address'][2];
		//返给前台js
		echo json_encode($addressData);exit;	
	}
	
	//修改地址数据
	public function addressEdit(){
		$addressModel = new Address;
	    if(IS_POST){
			//调用模型文件里的Type类的store方法去执行数据添加
			if($addressModel->edit()){
				//若模型类中数据验证正确，成功
				View::success('收货地址修改成功');
			}
			//提示错误
			View::error($addressModel->getError());
		}
	}
	
	//显示默认地址
	public function defaultAddressShow(){
		$uaid = q('post.u');
		$uid = $_SESSION['user']['uid'];
		$addressModel = new Address;
		//先让当前用户的所有地址 de_address 都变为0
		$addressModel->where('user_uid',$uid)->update(['de_address'=>0]);
		//让当前选中的 de_address 设为1
		$addressModel->where('uaid',$uaid)->update(['de_address'=>1]);		
	}
	
	//删除地址
	public function delAddress(){
		$uaid = q('get.uaid',null,'intval');
		$addressModel = new Address;
		$addressModel->where('uaid',$uaid)->delete();
		View::success('地址删除成功');
	}
}
